package com.example.administrator.siku.fragments;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.siku.R;
import com.example.administrator.siku.goods.fragment.KudashiFragment;
import com.example.administrator.siku.goods.fragment.ShaihuoFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsFragment extends Fragment {

    private View view;
    private TabLayout tablayout;
    private ViewPager vp;
    private List<Fragment> list;
    private List<String> titles;
    private MyFragmentAdapter myadapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_goods, container, false);
        initView();

        list = new ArrayList<>();

        ShaihuoFragment shaihuoFragment = new ShaihuoFragment();
        KudashiFragment kudashiFragment = new KudashiFragment();
        list.add(shaihuoFragment);
        list.add(kudashiFragment);


        titles = new ArrayList<>();

        titles.add("晒货");
        titles.add("库大师");

        myadapter=new MyFragmentAdapter(getChildFragmentManager());
        vp.setAdapter(myadapter);

        //和tableyout关联

        tablayout.setupWithViewPager(vp);
//        vp.setOffscreenPageLimit(3);

        return view;
    }

    private void initView() {
        tablayout = ((TabLayout) view.findViewById(R.id.fragment_goods_tablayout));
        vp = ((ViewPager) view.findViewById(R.id.fragment_goods_viewpager));
    }

    class MyFragmentAdapter extends FragmentPagerAdapter {



        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size()>0?list.size():0;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
}